<template>
  <div class="com-container">
    <!-- 标题容器 -->
    <div class="title" :style="comStyle">
      <span>{{'▎ ' + showTitle}}</span>
      <span class="iconfont title-icon" @click="showChoice = !showChoice" :style="comStyle">&#xe6eb;</span>
      <div class="select-con" v-show="showChoice" :style="[marginStyle, comStyle]">
        <div class="select-item" v-for="item in selectTypes" :key="item.key"
        @click="handleSelect(item.key)">{{item.text}}</div>
      </div>
    </div>
    <!-- 图表容器 -->
    <div class="com-chart" ref="trendRef"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_utils'
export default {
  data () {
    return {
      chartInstance: null,
      allData: null,
      // 是否显示 下拉选项
      showChoice: false,
      // 显示的数据类型
      choiceType: 'map',
      titleFontSize: 0
    }
  },
  created () {
    // 组件创建完成后，进行回调函数的注册
    this.$socket.registerCallBack('trendData', this.getData)
  },
  mounted () {
    this.initChart()
    // this.getData()
    // 发送数据到服务器，让服务器响应所需数据回来
    this.$socket.send({
      action: 'getData',
      socketType: 'trendData',
      chartName: 'trend',
      value: ''
    })
    window.addEventListener('resize', this.screenAdapter)
    // 页面首次加载，主动适配屏幕
    this.screenAdapter()
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    // 组件销毁时，进行回调函数注销
    this.$socket.unRegisterCallBack('trendData')
  },
  // 当需要使用一项数据，但 这项数据 又依赖于 组件data中某个数据，这时可以使用 计算属性 进行维护
  computed: {
    selectTypes () {
      if (!this.allData) {
        return []
      }
      return this.allData.type.filter(item => item.key !== this.choiceType)
    },
    showTitle () {
      if (!this.allData) {
        return ''
      }
      return this.allData[this.choiceType].title
    },
    // 设置给标题的样式
    comStyle () {
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor,
        backgroundColor: getThemeValue(this.theme).backgroundColor
      }
    },
    marginStyle () {
      return { marginLeft: this.titleFontSize + 'px' }
    },
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      this.chartInstance.dispose()
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    }
  },
  methods: {
    initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.trendRef, this.theme)
      const initOption = {
        // 配置坐标轴的位置
        grid: {
          left: '3%',
          top: '35%',
          right: '4%',
          bottom: '1%',
          // 距离 的计算 从坐标轴文字 开始
          containLabel: true
        },
        tooltip: {
          // 触发类型
          trigger: 'axis'
        },
        legend: {
          left: 20,
          top: '15%',
          icon: 'circle'
        },
        xAxis: { type: 'category', boundaryGap: false },
        yAxis: { type: 'value' }
      }
      this.chartInstance.setOption(initOption)
    },
    // res为服务端发送给客户端 的图表所需数据
    getData (res) {
      // const { data: res } = await this.$http.get('trend')
      this.allData = res
      console.log(this.allData)
      this.updateChart()
    },
    updateChart () {
      // 半透明的颜色值
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)'
      ]
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)'
      ]
      // 数据处理
      // 类目轴数据
      const timeArr = this.allData.common.month
      // y轴数值轴 数据
      const valueArr = this.allData[this.choiceType].data
      const seriesArr = valueArr.map((item, index) => {
        return {
          type: 'line',
          data: item.data,
          stack: this.choiceType,
          name: item.name,
          // 显示为 区域面积图
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: colorArr1[index] },
              { offset: 1, color: colorArr2[index] }
            ])
          }
        }
      })
      // 图例的数据
      const legendArr = valueArr.map(item => item.name)
      const dataOption = {
        xAxis: { data: timeArr },
        legend: {
          data: legendArr
        },
        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter () {
      this.titleFontSize = this.$refs.trendRef.offsetWidth / 100 * 3.6
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize / 1.5,
          itemHeight: this.titleFontSize / 1.5,
          itemGap: this.titleFontSize / 1.5,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    // 选择 其它类型 的趋势图
    handleSelect (currentType) {
      this.choiceType = currentType
      this.updateChart()
      this.showChoice = false
    }
  }
}
</script>

<style lang="less" scoped>
  .title {
    position: absolute;
    left: 20px;
    top: 20px;
    z-index: 10;
    color: #fff;
    .title-icon {
      margin-left: 10px;
      cursor: pointer;
    }
    .select-con {
      background-color: #222733;
      .select-item:hover {
        cursor: pointer;
        color: skyblue;
      }
    }
  }
</style>
